﻿using Dapper;
using OMG.Fill.Potato.Model.Ent;
using ProjectShare;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
namespace OMG.Fill.Potato.Services
{
    /// <summary>
    /// 角色表T_Role通用操作类
    /// </summary>
    public class Roles
    {
        /// <summary>
        /// 添加
        /// </summary>
        public virtual ResResult<int> AddInfo(RoleEntity model) {
            ResResult<int> obj = new ResResult<int>();
            string strSql = @"INSERT INTO dbo.T_Role";
            strSql +="(";
            strSql +="Id,";
            strSql +="RoleName,";
            strSql +="RoleType,";
            strSql +="Status,";
            strSql +="Sort,";
            strSql +="Remark)";
            strSql +=" values(";
            strSql +="@Id,";
            strSql +="@RoleName,";
            strSql +="@RoleType,";
            strSql +="@Status,";
            strSql +="@Sort,";
            strSql +="@Remark)";
            try
            {
                using (var dbCon = ConnectFactory.GetOpenConnection())
                {
                    if (!CheckName(model.Id, model.RoleName))
                    {
                        model.Id = CustomFun.GetGuid();
                        int result = dbCon.Execute(strSql, model);
                        if (result > 0)
                        {
                            obj.model = result;
                            obj.status = DefaultKey.SuccessResult;
                            obj.message = "添加成功！";
                        }
                        else
                        {
                            obj.model = result;
                            obj.status = DefaultKey.InfoResult;
                            obj.message = "添加失败！数量为:" + result;
                        }
                    }
                    else
                    {
                        obj.model = 0;
                        obj.status = DefaultKey.InfoResult;
                        obj.message = "角色名称重复";
                    }
                    dbCon.Dispose();//数据库连接释放
                    return obj;
                }
            }
            catch (Exception ex)
            {
                obj.status = DefaultKey.ErrorResult;
                obj.message = ex.ToString().Trim();
                LogHelper.ErrorLog("新增角色表T_Role对象信息失败", ex);
                return obj;
            }
        }
        /// <summary>
        /// 修改
        /// </summary>
        public virtual ResResult<int> UpdateInfo(RoleEntity model)
        {
            ResResult<int> obj = new ResResult<int>();
            string strSql = @"UPDATE dbo.T_Role set ";
            strSql+="RoleName = @RoleName,";
            strSql+="RoleType = @RoleType,";
            strSql+="Status = @Status,";
            strSql+="Sort = @Sort,";
            strSql+="Remark = @Remark";
            strSql+=" where Id = @Id";
           try
            {
                using (var dbCon = ConnectFactory.GetOpenConnection())
                {
                    if (!CheckName(model.Id, model.RoleName))
                    {
                        int result = dbCon.Execute(strSql, model);
                        if (result > 0)
                        {
                            obj.model = result;
                            obj.status = DefaultKey.SuccessResult;
                            obj.message = "更新成功！";
                        }
                        else
                        {
                            obj.model = result;
                            obj.status = DefaultKey.InfoResult;
                            obj.message = "更新失败！数量为:" + result;
                        }
                    }
                    else {
                        obj.model = 0;
                        obj.status = DefaultKey.InfoResult;
                        obj.message = "角色名称重复";
                    }
                    dbCon.Dispose();//数据库连接释放
                    return obj;
                }
            }
            catch (Exception ex)
            {
                obj.status = DefaultKey.ErrorResult;
                obj.message = ex.ToString().Trim();
                LogHelper.ErrorLog("更新角色表T_Role对象信息失败", ex);
                return obj;
            }
        }
        /// <summary>
        /// 检查角色名称是否重复
        /// </summary>
        /// <param name="id"></param>
        /// <param name="account"></param>
        /// <returns></returns>
        private bool CheckName(string id, string RoleName)
        {
            try
            {
                using (var dbCon = ConnectFactory.GetOpenConnection())
                {
                    if (id == "0")
                    {
                        string strSql = "select count(1) from dbo.T_Role where RoleName=@RoleName";
                        if (dbCon.Query<int>(strSql, new { RoleName = RoleName }).FirstOrDefault() == 1)
                        {
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                    else
                    {
                        string strSql = "select count(1) from dbo.T_Role where RoleName=@RoleName and Id!=@Id";
                        if (dbCon.Query<int>(strSql, new { RoleName = RoleName, Id = id }).FirstOrDefault() == 1)
                        {
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        public virtual ResResult<int> DeleteInfo(string id)
        {
            ResResult<int> obj = new ResResult<int>();
            string strSql = @"delete from dbo.T_Role where Id = @Id";
            int result;
            try
            {
                using (var dbCon = ConnectFactory.GetOpenConnection())
                {
                    result = dbCon.Execute(strSql, new { Id = id });
                    if (result > 0)
                    {
                        obj.model = result;
                        obj.status = DefaultKey.SuccessResult;
                        obj.message = "删除成功！";
                    }
                    else
                    {
                        obj.model = result;
                        obj.status = DefaultKey.InfoResult;
                        obj.message = "删除失败！数量为:" + result;
                    }
                    dbCon.Dispose();//数据库连接释放
                    return obj;
                }
            }
            catch (Exception ex)
            {
                obj.status = DefaultKey.ErrorResult;
                obj.message = ex.ToString().Trim();
                LogHelper.ErrorLog("删除角色表T_Role对象信息失败", ex);
                return obj;
            }
        }
        /// <summary>
        /// 批量删除
        /// </summary>
        public virtual ResResult<int> DeleteBatch(string[] ids)
        {
            ResResult<int> obj = new ResResult<int>();
            string strIds = string.Join(",", ids.Select(x => x.ToString()).ToArray());
            string strSql = @"delete from dbo.T_Role where Id in @ids";
            var dynamicSqlParam = new DynamicParameters();
            dynamicSqlParam.Add("ids", ids.ToArray(), null, null, null);
            int result;
            try
            {
                using (var dbCon = ConnectFactory.GetOpenConnection())
                {
                    result = dbCon.Execute(strSql, dynamicSqlParam);
                    if (result > 0)
                    {
                        obj.model = result;
                        obj.status = DefaultKey.SuccessResult;
                        obj.message = "删除成功！";
                    }
                    else
                    {
                        obj.status = DefaultKey.InfoResult;
                        obj.message = "删除失败！数量为:" + result;
                    }
                    dbCon.Dispose();//数据库连接释放
                    return obj;
                }
            }
            catch (Exception ex)
            {
                LogHelper.ErrorLog("获取数据字典表批量删除角色表T_Role表失败", ex);
                obj.message = ex.ToString().Trim();
                obj.status = DefaultKey.ErrorResult;
                return obj;
            }
        }
        /// <summary>
        /// 获取指定对象
        /// </summary>
        public virtual ResResult<RoleEntity> GetModel(string id) {
            ResResult<RoleEntity> obj = new ResResult<RoleEntity>();
            string strSql = @"select * from dbo.T_Role where Id = @Id";
            try
            {
                using (var dbCon = ConnectFactory.GetOpenConnection())
                {
                    obj.model = dbCon.Query<RoleEntity>(strSql, new { Id = id }).FirstOrDefault();
                    obj.status = DefaultKey.SuccessResult;
                    obj.message = "获取成功！";
                    dbCon.Dispose();//数据库连接释放
                    return obj;
                }
            }
            catch (Exception ex)
            {
                obj.status = DefaultKey.ErrorResult;
                obj.message = ex.ToString().Trim();
                LogHelper.ErrorLog("获取角色表T_Role对象信息失败", ex);
                return obj;
            }
        }
        /// <summary>
        /// 获取全部列表
        /// </summary>
        /// <returns></returns>
        public virtual ResResult<List<RoleEntity>> GetList()
        {
            ResResult<List<RoleEntity>> obj = new ResResult<List<RoleEntity>>();
            string strSql = @"select * from dbo.T_Role";
            try
            {
                using (var dbCon = ConnectFactory.GetOpenConnection())
                {
                    obj.model = dbCon.Query<RoleEntity>(strSql).ToList();
                    obj.status = DefaultKey.SuccessResult;
                    obj.message = "获取成功！";
                    dbCon.Dispose();//数据库连接释放
                    return obj;
                }
            }
            catch (Exception ex)
            {
                obj.status = DefaultKey.ErrorResult;
                obj.message = ex.ToString().Trim();
                LogHelper.ErrorLog("更新角色表T_Role对象信息失败", ex);
                return obj;
            }
        }
    }
}